<template>
  <div>
    <el-form
      :model="postForm"
      ref="postForm"
      :rules="rules"
      label-width="120px"
      label-suffix=":"
    >
      <sticky :z-index="10" :class-name="'sub-navbar ' + postForm.status">
        <el-button v-loading="loading" style="margin-left: 10px;" type="success" @click="submitForm">
          保存
        </el-button>
      </sticky>

      <div style="padding: 15px">
        <el-row>
          <el-col :span="22">
            <el-form-item label="提示文字">
              <el-input v-model.trim="postForm.text"/>
            </el-form-item>
            <el-form-item label="公众号二维码">
              <upload-image
                bucket="qrcode"
                :max="10"
                :max-width="375"
                :quality="50"
                @onSuccess="onSuccess"
                @onRemove="onRemove"
                :file-list="postForm.image"
              />
              <span>可以上传最多10张， 建议尺寸 375 x 667 px</span>
            </el-form-item>
          </el-col>
        </el-row>
      </div>

    </el-form>
  </div>
</template>

<script>
import Sticky from '@/components/Sticky'
import UploadImage from '@/components/Upload/UploadImage'
import api from '@/api'
export default {
  name: 'SystemQrcode',
  components: {
    Sticky,
    UploadImage
  },
  data() {
    return {
      postForm: {
        status: 'draft'
      },
      rules: {
      },
      loading: false
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    async fetchData() {
      const { data } = await api.system.fetchQrcode()
      this.postForm = Object.assign({}, data)
      this.postForm.status = 'publisher'
      // 图片
      if (this.postForm.images) {
        const list = []
        this.postForm.images.forEach(x => {
          list.push({
            id: +x.id,
            // 这个没有缩略图
            url: x.path
          })
        })
        this.postForm.image = JSON.parse(JSON.stringify(list))
      } else {
        this.postForm.image = []
      }
    },
    submitForm() {
      this.$refs.postForm.validate(validate => {
        if (validate) {
          this.loading = true
          // 图片
          let image = []
          if (this.postForm.image && this.postForm.image.length > 0) {
            image = this.postForm.image.map(x => x.id)
          }
          api.system.saveQrcode({
            ...this.postForm,
            image
          }).then(response => {
            this.$notify({
              title: '成功',
              message: response.message || '保存成功',
              type: 'success',
              duration: 2000
            })
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        }
      })
    },
    /**
     * 图片上传
     */
    onSuccess(data, fileList) {
      if (data && data.id) {
        this.postForm.image.push({
          id: data.id,
          url: data.full_name
        })
        // this.submitForm()
      }
    },
    /**
     * 删除图片
     */
    onRemove(data, fileList) {
      if (data && data.id) {
        this.postForm.image = this.postForm.image.filter(x => x.id !== data.id)
        this.submitForm()
      }
    }
  }
}
</script>
